<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }
        
        body {
            text-align: center;
        }
        
        button {
            margin-top: 50px;
            width: 100px;
            height: 50px;
            background-color: pink;
            font-size: 1.5rem;
        }
        
        #box {
            overflow: hidden;
            border: 3px solid #000;
        }
        
        #box div {
            float: left;
            width: 150px;
            height: 60px;
            color: #fff;
            text-align: center;
            line-height: 60px;
        }
    </style>
</head>

<body>
    <button class="Run">Run</button>
    <button class="Stop">Stop</button>
    <div id="box"></div>
    <script>
        //获取dom元素
        var oRun = document.getElementsByClassName('Run')[0];
        var oStop = document.getElementsByClassName('Stop')[0];
        var oBox = document.getElementById('box');
        var timer = 0;
        //设置btn的点击事件
        oRun.onclick = function() {
            clearInterval(timer);
            oRun.style.backgroundColor = 'pink';
            oRun.style.color = '#000';
            timer = setInterval(function() {
                //创建div标签
                var oDiv = document.createElement('div');
                //设置rgb颜色随机
                // var r = Math.floor(Math.random() * 256);
                // var g = Math.floor(Math.random() * 256);
                // var b = Math.floor(Math.random() * 256);
                // oDiv.style.backgroundColor = 'rgb(' + r + ',' + g + ',' + b + ')';
                //设置16进制的颜色值
                oDiv.style.backgroundColor = '#' + Math.random().toString(16).slice(2, 8);
                //添加oDiv节点
                oBox.appendChild(oDiv);
                //添加rgb颜色值
                // oDiv.innerHTML = 'rgb(' + r + ',' + g + ',' + b + ')';
                //添加16进制的颜色值
                oDiv.innerHTML = '#' + Math.random().toString(16).slice(2, 8);
                oRun.style.backgroundColor = '#5a0de3';
                oRun.style.color = '#fff';
            }, 1000);
        }
        oStop.onclick = function() {
            clearInterval(timer);
            oRun.style.backgroundColor = 'pink';
            oRun.style.color = '#000';
        }
    </script>
</body>

</html>